package gom.example.gramediamajalah;

import java.nio.channels.AsynchronousCloseException;
import java.util.ArrayList;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class AksesServerActivity extends Activity {

	
	private static final String AR_ID = "id";
	private static final String AR_TITLE = "title";
	private static final String AR_SHORT_DESCRIPTION = "short_description";
	private static final String AR_IMAGE_FILENAME = "image_filename";
	private static final String AR_CONTENT="content";

	JSONArray artikel = null;

	ArrayList<HashMap<String, String>> daftar_artikel = new ArrayList<HashMap<String, String>>();

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_list_item);

		JSONParser jParser = new JSONParser();

		JSONObject json = jParser.getJSONFromUrl("");

		try {
			artikel = json.getJSONArray("artikel");

			for (int i = 0; i < artikel.length(); i++) {

				JSONObject ar = artikel.getJSONObject(i);

				String id = ar.getString(AR_ID);
				String title = ar.getString(AR_TITLE);
				String short_desciption=ar.getString(AR_SHORT_DESCRIPTION);
				String image_filename=ar.getString(AR_IMAGE_FILENAME);
				String content = ar.getString(AR_CONTENT).substring(0, 100)
						+ "...(baca selengkapnya)";

				HashMap<String, String> map = new HashMap<String, String>();

				map.put(AR_ID, id);
				map.put(AR_TITLE, title);
				map.put(AR_SHORT_DESCRIPTION, short_desciption);
				map.put(AR_IMAGE_FILENAME, image_filename);
				

				map.put(AR_CONTENT, content);

				daftar_artikel.add(map);

			}

		} catch (JSONException e) {

			e.printStackTrace();

		}

		this.adapter_listview();

	}
	
	private class AksesServer extends AsyncTask<String,Void, String>{

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}
		
	}
		
	


	
		

		

		

			@Override
			public void onItemClick(AdapterView<?> ViewGrup parent,
					int position, long id) {

				String kode = ((TextView) view.findViewById(R.id.action_settings))
						.getText().toString();

				Intent in = new Intent(AksesServerActivity.this,
						DetailAksesServer.class);

				in.putExtra(AR_ID, kode);

				startActivity(in);

			}

		});

	}

}
